package org.vectrics.dataweb;


import java.io.ByteArrayInputStream;
import org.vectrics.VectricsException;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilderFactory;


public class RequestUtils {
	private transient static Logger log = Logger.getLogger(RequestUtils.class);
	
	public static Element getDataElement(String dataXml) throws VectricsException {
		log.debug("processRequest(request) - creating data element");
		Document requestDataDocument = null;
	
		if (dataXml != null) {
			log.debug("processRequest(request) - parsing data");
			String xmlStr = dataXml;
			try {
				ByteArrayInputStream requestStream = new ByteArrayInputStream(xmlStr.getBytes());
				requestDataDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(requestStream);
			}
			catch (Exception ex) {
				log.error("Exception parsing request data", ex);
				throw new VectricsException("Exception parsing request data", ex);
			}
			
		}
		return(requestDataDocument.getDocumentElement());
	}

	
	public static Element getAuthUserElement(Element requestDataElement) {
		Element authElement = null;
		if (requestDataElement != null) {
			NodeList list = requestDataElement.getElementsByTagName("auth");
			
			if (list.getLength() > 0) {
				log.debug("Found authentication element");
				//				UserManager mgr = new UserManager();
				
				authElement = (Element)list.item(0);
			}
		}
		return(authElement);
	}
	


}
